package bg.openid.joid;

// bg.openid.joid.Bean
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

public class Bean {

	private String comment = "Comment bean";
	private String identifier = "";

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment += "<hr/>\n" + comment;
	}

	public Bean() {

	}

	public String toString() {
		return "Bg Bean ";
	}

	public void addComment(String string) {
		this.comment += "\n<br/>----------------<br/> " + string;

	}

	public void addComment(Object k, Object s) {
		this.addComment("<span style='color: red; font-weight: bolder;'> " + k + " </span>" + s);

	}

	public void addException(Throwable t) {
		comment += "<br/>-------------- <br/>";
		comment += " Exception: " + t.getClass() + "<br/>";
		comment += " " + t.getMessage() + "<br/>";
		StackTraceElement[] stes = t.getStackTrace();
		for (StackTraceElement ste : stes) {
			comment += "<br/>" + ste;
		}
		if (t.getCause() == null) {
			comment += " <br/> ---------------------";
		} else {
			comment += " <p/> cause by: ";
			addException(t.getCause());
		}
	}

	@SuppressWarnings("unchecked")
	public void display(List parameters) {

		Iterator iterator = parameters.iterator();
		while (iterator.hasNext()) {
			Object parameter = iterator.next();
			addComment("ParameterList :: " + parameter);
		}

	}

	public void display(Properties p) {
		Iterator iterator = p.keySet().iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			addComment(key, "" + p.get(key));
		}

	}

	public void display(HttpServletRequest request) {
		this.addComment("Method: ", request.getMethod());
		Map params = request.getParameterMap();
		this.display(params);
	}

	public void displayUrl(String destinationURL) {
		try {
			StringTokenizer st1 = new StringTokenizer(destinationURL, "?");
			this.addComment("destinationURL.host", st1.nextToken());
			String params = st1.nextToken();
			params = URLDecoder.decode(params);
			StringTokenizer st2 = new StringTokenizer(params, "&");

			while (st2.hasMoreTokens()) {
				try {
					String s = st2.nextToken();
					StringTokenizer st3 = new StringTokenizer(s, "=");
					String key = st3.nextToken();
					String param = st3.nextToken();
					param = URLDecoder.decode(param);
					this.addComment("destinationURL.param <span style='color: blue; font-weight: bolder;'> " + key + "</span> " + param);
				} catch (Exception e) {
					this.addComment("ExeceptionAAA");
				}
			}
		} catch (Exception e) {
			this.addComment("ExeceptionBBB");
		}
	}

	private void display(Map params) {
		Set keys = params.keySet();
		for (Object k : keys) {
			Object v = params.get(k);
			if (v.getClass().isArray()) {
				Object[] vv = (Object[]) v;
				for (Object o : vv) {
					if (o instanceof String[]) {
                        String[] oo = (String[]) o;
                        for(String ss : oo){
                        	this.addComment(k, ss);
                        }                        
					} else {
						this.addComment(k, o+" :: "+o.getClass());
					}
				}
			} else {
				this.addComment(k, v);
			}
		}
	}

	public void clean() {
		this.comment = "clean <hr/>";

	}
}